//
//  LSDatePickerView.m
//  LSDatePickerViewDemo
//
//  Created by Shawn Li on 15/11/9.
//  Copyright (c) 2015年 Shawn Li. All rights reserved.
//

#import "LSDatePickerView.h"
#import "UIColor+HexString.h"

@interface LSDatePickerView ()
@property (nonatomic, strong) NSString *selectDate;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UIButton *comfirmBtn;
@property (nonatomic, strong) NSString  *dateFormatter;
@end

@implementation LSDatePickerView
+ (LSDatePickerView *)instanceDatePickerView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"LSDatePickerView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}

- (void)awakeFromNib
{
    self.bgView.layer.cornerRadius = 5;
    self.bgView.layer.borderWidth = 1;
    self.bgView.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.bgView.layer.masksToBounds = YES;
    
    /** 确定 */
    self.comfirmBtn.layer.cornerRadius = 3;
    self.comfirmBtn.layer.borderWidth = 1;
    self.comfirmBtn.layer.borderColor = [[UIColor colorwithHexString:@"5AC33B"] CGColor];
    self.comfirmBtn.layer.masksToBounds = YES;
    
    /** 取消按钮 */
    self.cancelBtn.layer.cornerRadius = 3;
    self.cancelBtn.layer.borderWidth = 1;
    self.cancelBtn.layer.borderColor = [[UIColor colorwithHexString:@"BAB9B9"] CGColor];
    self.cancelBtn.layer.masksToBounds = YES;

//    self.datePicker.timeZone = [[NSTimeZone alloc] initWithName:@"GTM+8"];//设置时区
//    self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];//设置日期选择控件的地区
    self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;//设置Mode格式
}

#pragma mark - 设置分钟时间间隔
- (void)setMinuteInterval:(NSInteger)minuteInterval {
    self.datePicker.minuteInterval = minuteInterval;
}

- (void)setIsBeforeTime:(BOOL)isBeforeTime {
    if (!isBeforeTime) {
        self.datePicker.minimumDate = [NSDate date];
    } else {
        self.datePicker.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];
    }
}

- (void)setDatePickerType:(DatePickerType)datePickerType {
    switch (datePickerType) {
        case DatePickerTypeOfDate://只显示日期
            self.datePicker.datePickerMode = UIDatePickerModeDate;
            self.dateFormatter = @"yyyy-MM-dd";
            break;
        case DatePickerTypeOfTime://只显示时间
            self.datePicker.datePickerMode = UIDatePickerModeTime;
            self.dateFormatter = @"HH:mm:ss";
            break;
        case DatePickerTypeOfDateAndTime://显示时间和日期
            self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
            self.dateFormatter = @"yyyy-MM-dd HH:mm:ss";
            break;
            
        default:
            break;
    }
}


- (NSString *)timeFormat
{
    NSDate *selected = [self.datePicker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:self.dateFormatter];
    NSString *currentOlderOneDateStr = [dateFormatter stringFromDate:selected];
    return currentOlderOneDateStr;
}

- (void)animationbegin:(UIView *)view {
    /* 放大缩小 */
    
    // 设定为缩放
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    // 动画选项设定
    animation.duration = 0.1; // 动画持续时间
    animation.repeatCount = -1; // 重复次数
    animation.autoreverses = YES; // 动画结束时执行逆动画
    
    // 缩放倍数
    animation.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
    animation.toValue = [NSNumber numberWithFloat:0.9]; // 结束时的倍率
    
    // 添加动画
    [view.layer addAnimation:animation forKey:@"scale-layer"];
    
}

- (IBAction)removeBtnClick:(id)sender {
    // 开始动画
    [self animationbegin:sender];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (IBAction)sureBtnClick:(id)sender {
    // 开始动画
    [self animationbegin:sender];
    
    self.selectDate = [self timeFormat];
    
    //delegate
    [self.delegate getSelectDate:self.selectDate type:self.type];
    
    
    [self removeBtnClick:nil];
}

@end
